﻿namespace Smart.Utils.System2
{
    using System;
    using System.Management;
    using System.Windows.Forms;

    public class SystemInfoHelper
    {
        public static bool UseBaseBoardManufacturer;
        public static bool UseBaseBoardProduct;
        public static bool UseBiosManufacturer;
        public static bool UseBiosVersion;
        public static bool UseDiskDriveSignature;
        public static bool UsePhysicalMediaSerialNumber;
        public static bool UseProcessorID;
        public static bool UseVideoControllerCaption;
        public static bool UseWindowsSerialNumber;

        public static string GetSoftware(string Software)
        {
            string str = "";
            int num = 0;
            for (int i = 0; i < Software.Length; i++)
            {
                if (Software[i] == ' ')
                {
                    num = 0;
                }
                if ((Software[i] != ' ') & (num == 0))
                {
                    str = str + Software[i].ToString();
                    num = 1;
                }
            }
            return str;
        }

        public static string GetSystemInfo()
        {
            string st = "";
            if (UseProcessorID)
            {
                st = st + RunQuery("Processor", "ProcessorId");
            }
            if (UseBaseBoardProduct)
            {
                st = st + RunQuery("BaseBoard", "Product");
            }
            if (UseBaseBoardManufacturer)
            {
                st = st + RunQuery("BaseBoard", "Manufacturer");
            }
            if (UseDiskDriveSignature)
            {
                st = st + RunQuery("DiskDrive", "Signature");
            }
            if (UseVideoControllerCaption)
            {
                st = st + RunQuery("VideoController", "Caption");
            }
            if (UsePhysicalMediaSerialNumber)
            {
                st = st + RunQuery("PhysicalMedia", "SerialNumber");
            }
            if (UseBiosVersion)
            {
                st = st + RunQuery("BIOS", "Version");
            }
            if (UseWindowsSerialNumber)
            {
                st = st + RunQuery("OperatingSystem", "SerialNumber");
            }
            st = RemoveUseLess(st);
            if (st.Length < 0x20)
            {
                return GetSystemInfo(st);
            }
            return st.Substring(0, 0x20).ToUpper();
        }

        public static string GetSystemInfo(string SoftwareName)
        {
            if (UseProcessorID)
            {
                SoftwareName = SoftwareName + RunQuery("Processor", "ProcessorId");
            }
            if (UseBaseBoardProduct)
            {
                SoftwareName = SoftwareName + RunQuery("BaseBoard", "Product");
            }
            if (UseBaseBoardManufacturer)
            {
                SoftwareName = SoftwareName + RunQuery("BaseBoard", "Manufacturer");
            }
            if (UseDiskDriveSignature)
            {
                SoftwareName = SoftwareName + RunQuery("DiskDrive", "Signature");
            }
            if (UseVideoControllerCaption)
            {
                SoftwareName = SoftwareName + RunQuery("VideoController", "Caption");
            }
            if (UsePhysicalMediaSerialNumber)
            {
                SoftwareName = SoftwareName + RunQuery("PhysicalMedia", "SerialNumber");
            }
            if (UseBiosVersion)
            {
                SoftwareName = SoftwareName + RunQuery("BIOS", "Version");
            }
            if (UseWindowsSerialNumber)
            {
                SoftwareName = SoftwareName + RunQuery("OperatingSystem", "SerialNumber");
            }
            SoftwareName = RemoveUseLess(SoftwareName);
            return SoftwareName;
        }

        private static string RemoveUseLess(string st)
        {
            for (int i = st.Length - 1; i >= 0; i--)
            {
                char ch = char.ToUpper(st[i]);
                if (((ch < 'A') || (ch > 'Z')) && ((ch < '0') || (ch > '9')))
                {
                    st = st.Remove(i, 1);
                }
            }
            return st;
        }

        private static string RunQuery(string TableName, string MethodName)
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_" + TableName);
            foreach (ManagementObject obj2 in searcher.Get())
            {
                try
                {
                    return obj2[MethodName].ToString();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
            return "";
        }

        public static void SetDefault()
        {
            UseBaseBoardManufacturer = false;
            UseBaseBoardProduct = true;
            UseBiosManufacturer = true;
            UseBiosVersion = false;
            UseDiskDriveSignature = false;
            UsePhysicalMediaSerialNumber = false;
            UseProcessorID = true;
            UseVideoControllerCaption = false;
            UseWindowsSerialNumber = false;
        }
    }
}

